// pages/personal_center/tea_leavedata/tea_leavedata.js
//引入封装请求
import request from "../../../utils/request";
const timeDate = require('../../../utils/util.js') //引入时间戳转换日期格式函数
Page({
  data: {
    tabs: ["请假", "出差"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    leaveData: [], //历史请假数据
    leaveDataEve: [], //历史出差数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    /* 取出登录信息 字符串*/
    let userInfo = wx.getStorageSync('userInfo');
    //转换为数组
    let UserInfo = JSON.parse(userInfo)
    this.setData({
      /* 转完之后 存值当前页面  */
      userInfo: UserInfo,
    })
  },
  //头部菜单
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },

  //请假详情点击事件
  details:async function (e) {
    let teaLeaveID = e.currentTarget.dataset.leaveid;

    //请假详情全部数据, 教师名称 请假时间 请假原因  销假时间 请假类型名称 请假状态 审核人 不批准原因 结束时间 销假状态 请假出差状态(0请假 1出差）
    let leaveData = [],teacherName, leaDate,leaCause, pseuTime, leaTypeName, leaState, auditor,remark, endTime,remStatus, isoverdue = false,leave=0;

    //请求根据请假Id查询请假数据
    let data = await request('/WxInterface/SelectLeaveTeaByID', {
      teaLeaveID
    });
    
    if (data.State) {
      data = data.Object;
      leaState = data.leaState; //请假状态
      teacherName = data.teacherName; //教师名称
      auditor = data.auditor; //审核人
      remark = data.remark; //不批准原因
      leaDate = data.leaDate.substr(6, 13); //开始时间
      endTime = data.endTime.substr(6, 13); //结束时间
      leaTypeName = data.leaTypeName; //请假类型
      leaCause = data.leaCause; //请假事由
      pseuTime = data.pseuTime; //销假时间
      remStatus = data.remStatus; //销假状态

      //开始与结束时间 时间戳转换日期格式 
      leaDate = timeDate.formatDate(leaDate, 'Y-M-D h:m:s');
      endTime = timeDate.formatDate(endTime, 'Y-M-D h:m:s');

      //销假时间 时间戳转换日期格式 
      if (pseuTime != null) {
        pseuTime=pseuTime.substr(6,13)
        pseuTime = timeDate.formatDate(pseuTime, 'Y-M-D h:m:s');
      }

      //请假审核状态 是否已过期
      let end = new Date(endTime).getTime();
      let newDate = new Date().getTime();
      if (end <= newDate && leaState != 1 && leaState != 2) {
        isoverdue = true;
      }

      leaveData = [leave, teacherName, leaDate,leaCause, pseuTime, leaTypeName, leaState, auditor,remark, endTime,remStatus, isoverdue]
    } else {
      wx.showToast({
        title: data.Text,
        icon: 'none',
        duration: 1800
      })
    }

    wx.setStorageSync('leaveDataTea', JSON.stringify(leaveData)) //转成string格式
    if (teaLeaveID > 0) {
      wx.navigateTo({ //reLaunch
        url: '/pages/personal_center/tea_leavedetails/tea_leavedetails'
      })
    } else {
      wx.showToast({
        title: "参数异常",
        icon: 'none',
        duration: 1800
      })
    }

   },
   //出差详情点击事件
  detailsEve:async function (e) {
    let evectionID = e.currentTarget.dataset.evectionid;

    //请假详情全部数据, 教师名称 批准人姓名 出差起始时间 出差所在地 出差原因 预支费用 销假时间 出差状态 不批准原因 出差结束时间 销假状态 请假出差状态(0请假 1出差）
    let leaveDataEve = [],teacherName,auditor,eveDate,eveAddress,eveReason,eveMoney,pseuTime,eveState,remark,endTime,remStatus, isoverdue = false,leave=1;

    //请求根据请假Id查询请假数据
    let data = await request('/WxInterface/SelectLeaveTeaByIDEve', {
      evectionID
    });
    if (data.State) {
      data = data.Object;

      eveState = data.eveState; //出差状态
      eveReason = data.eveReason; //出差原因
      eveAddress = data.eveAddress; //出差所在地
      auditor = data.auditor; //批准人姓名
      teacherName = data.teacherName; //老师姓名
      eveDate = data.eveDate.substr(6, 13); //开始时间
      endTime = data.endTime.substr(6, 13); //结束时间
      eveMoney = data.eveMoney; //预支费用
      remark = data.remark; //不批准原因
      pseuTime = data.pseuTime; //销假时间
      remStatus = data.remStatus; //销假状态

      //开始与结束时间 时间戳转换日期格式 
      eveDate = timeDate.formatDate(eveDate, 'Y-M-D h:m:s');
      endTime = timeDate.formatDate(endTime, 'Y-M-D h:m:s');

      //销假时间 时间戳转换日期格式 
      if (pseuTime != null) {
        pseuTime=pseuTime.substr(6,13)
        pseuTime = timeDate.formatDate(pseuTime, 'Y-M-D h:m:s');
      }

      //请假审核状态 是否已过期
      let end = new Date(endTime).getTime();
      let newDate = new Date().getTime();
      if (end <= newDate && eveState != 1 && eveState != 2) {
        isoverdue = true;
      }

      leaveDataEve = [leave,teacherName,auditor,eveDate,eveAddress,eveReason,eveMoney,pseuTime,eveState,remark,endTime,remStatus, isoverdue]
    } else {
      wx.showToast({
        title: data.Text,
        icon: 'none',
        duration: 1800
      })
    }

    wx.setStorageSync('leaveDataTea', JSON.stringify(leaveDataEve)) //转成string格式
    if (evectionID > 0) {
      wx.navigateTo({ //reLaunch
        url: '/pages/personal_center/tea_leavedetails/tea_leavedetails'
      })
    } else {
      wx.showToast({
        title: "参数异常",
        icon: 'none',
        duration: 1800
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: async function () {
    //请假信息(isoverdue请假是否已过期) //开始时间 请假类型名称 请假事由 开始 结束 审核状态 请假id 请假历史记录数据
    let startDate, leaTypeName, leaCause, start, end, appStatus, leaveID, leaveData = [],
      isoverdue = false;
    //出差信息  开始时间 出差事由 出差所在地 出差费用 开始 结束 审核状态 请假id 请假历史记录数据
    let eveDate, eveReason, eveAddress, eveMoney, startEve, endEve, eveState, evectionID, leaveDataEve = [],
      isoverdueEve = false;
    //获取本地储存的老师ID
    let teacherID = this.data.userInfo[0];

    // //发送请求获取请假信息
    let list = await request('/WxInterface/SelectTeacherDate', {
      teacherID
    });
    let listEve = await request('/WxInterface/SelectTeacherDateEve', {
      teacherID
    });

    //获取信息
    list = list.Object;
    listEve = listEve.Object;

    for (var i = 0; i < list.length; i++) {
      isoverdue = false;
      //开始时间 
      start = list[i].leaDate.substr(6, 13);
      startDate = timeDate.formatDate(start, 'Y-M-D h:m:s');

      leaTypeName = list[i].leaTypeName; //请假类型名称
      leaCause = list[i].leaCause; //请假事由
      leaveID = list[i].teaLeaveID; //请假Id

      //请假审核状态
      end = Number(list[i].endTime.substr(6, 13));
      let newDate = new Date().getTime();
      appStatus = list[i].leaState; //0:待批准，1批准通过，2待销假，3已销假
      if (end < newDate && appStatus != 1 && appStatus != 2) {
        isoverdue = true;
      }

      leaveData.push({
        isoverdue,
        startDate,
        appStatus,
        leaTypeName,
        leaCause,
        leaveID
      })
    }

    for (var i = 0; i < listEve.length; i++) {
      isoverdueEve = false;
      //开始时间 
      startEve = listEve[i].eveDate.substr(6, 13);
      eveDate = timeDate.formatDate(startEve, 'Y-M-D h:m:s');

      eveMoney = listEve[i].eveMoney; //出差费用
      eveAddress = listEve[i].eveAddress; //出差所在地
      eveReason = listEve[i].eveReason; //出差事由
      evectionID = listEve[i].evectionID; //出差Id

      //请假审核状态
      endEve = Number(listEve[i].endTime.substr(6, 13));
      let newDate = new Date().getTime();
      eveState = listEve[i].eveState; //0:待批准，1未通过，2 已通过
      if (endEve < newDate && eveState != 1 && eveState != 2) {
        isoverdueEve = true;
      }

      leaveDataEve.push({
        isoverdueEve,
        eveDate,
        eveState,
        eveMoney,
        eveAddress,
        eveReason,
        evectionID
      })
    }

    this.setData({
      leaveData: leaveData,
      leaveDataEve: leaveDataEve,
    })

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})